package com.topnav.mc.config;
import com.google.code.kaptcha.text.impl.DefaultTextCreator;
import net.sf.jsqlparser.expression.StringValue;
import org.springframework.stereotype.Component;

import java.util.*;

//
/**
 * 自定义Kaptcha的文本验证码，使用数学运算验证码
 * @Author: kenny
 * @Date: 2024/1/5 14:56
 */
@Component
public class CalculationTextCreator {//extends DefaultTextCreator {
    //@Override
    public String getText(){
        //List<String> list = new ArrayList<>();
        //计算式的结果
        StringBuilder text = new StringBuilder();
        Random random = new Random();
        int x = random.nextInt(100);
        int y = random.nextInt(100);
        //char[] operators={'+','-','*'};
        int operatorRandom = (int) Math.round(Math.random());
        //char operator=operators[operatorRandom];
        switch (operatorRandom){
            case 0:
                ///list.add(x+"+"+y+"=?@"+(x+y));
                //list.add(String.valueOf(x+y));
                text.append(x+"+"+y+"=?@"+(x+y));
                break;
            case 1:
                if (x>y) {
                    //list.add(x+"-"+y+"=?@"+(x-y));
                    //list.add(String.valueOf(x-y));
                    text.append(x+"-"+y+"=?@"+(x-y));
                }else{
                    //list.add(y+"-"+x+"=?@"+(y-x));
                    //list.add(String.valueOf(y-x));
                    text.append(y+"-"+x+"=?@"+(y-x));
                }
                break;
        }
        return text.toString();

    }
}
